package flc.ast.adapter;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import flc.ast.R;
import flc.ast.activity.CollectPicDetailActivity;
import flc.ast.activity.PicDetailActivity;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyImgBean;
import flc.ast.bean.MyImgItemBean;
import flc.ast.bean.MyPicAlbumBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.view.container.StkRecycleView;

public class SelPictureAdapter extends StkProviderMultiAdapter<MyImgBean> {

    public SelPictureAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(125));
        addItemProvider(new SelPictureProvider());
    }

    private boolean isEdit = false;

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private boolean isAll = false;

    public void setAll(boolean all) {
        isAll = all;
    }

    private boolean isAlbum = false;

    public void setAlbum(boolean album) {
        isAlbum = album;
    }

    private boolean isCollect = false;

    public void setCollect(boolean collect) {
        isCollect = collect;
    }

    private class SelPictureProvider extends BaseItemProvider<MyImgBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_picture;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyImgBean bean) {
            baseViewHolder.setText(R.id.tvSelPictureItemDate, bean.getDate() + "  (" + bean.getList().size() + ")");
            StkRecycleView recycleView = baseViewHolder.getView(R.id.rvSelPictureItemList);
            recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
            SelPictureItemAdapter itemAdapter = new SelPictureItemAdapter();
            recycleView.setAdapter(itemAdapter);
            itemAdapter.setEdit(isEdit);
            itemAdapter.setAll(isAll);
            // 使用流操作将数据按每 6 条一组进行分组
            Map<Integer, List<MyImgItemBean>> groups = new HashMap<>();
            int groupSize = 6;
            List<MyPicAlbumBean> list = new ArrayList<>();
            for (int i = 0; i < bean.getList().size(); i += groupSize) {
                if ((i + groupSize) <= bean.getList().size()) {
                    groups.put((i / groupSize), bean.getList().subList(i, i + groupSize));
                } else {
                    groups.put((i / groupSize), bean.getList().subList(i, bean.getList().size()));
                }
            }
            for (Map.Entry<Integer, List<MyImgItemBean>> entry : groups.entrySet()) {
                list.add(new MyPicAlbumBean(entry.getValue()));
            }
            itemAdapter.setList(list);
            itemAdapter.addChildClickViewIds(R.id.rlSelPicture1, R.id.rlSelPicture2, R.id.rlSelPicture3, R.id.rlSelPicture4, R.id.rlSelPicture5, R.id.rlSelPicture6);
            itemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    MyPicAlbumBean item = (MyPicAlbumBean) adapter.getItem(position);
                    switch (view.getId()) {
                        case R.id.rlSelPicture1://top图片1
                            setItemSel(item.getList().get(0), itemAdapter);
                            break;
                        case R.id.rlSelPicture2://top图片2
                            setItemSel(item.getList().get(1), itemAdapter);
                            break;
                        case R.id.rlSelPicture3://top图片3
                            setItemSel(item.getList().get(2), itemAdapter);
                            break;
                        case R.id.rlSelPicture4://top图片4
                            setItemSel(item.getList().get(3), itemAdapter);
                            break;
                        case R.id.rlSelPicture5://top图片5
                            setItemSel(item.getList().get(4), itemAdapter);
                            break;
                        case R.id.rlSelPicture6://top图片6
                            setItemSel(item.getList().get(5), itemAdapter);
                            break;
                    }
                }
            });
        }
    }

    //设置数据选中
    private void setItemSel(MyImgItemBean item, BaseQuickAdapter adapter) {
        if (isEdit) {
            if (item.isSelect()) {
                item.setSelect(false);
                clickFeedback.onClickFeedback(false, item.getPath());
            } else {
                item.setSelect(true);
                clickFeedback.onClickFeedback(true, item.getPath());
            }
            adapter.notifyDataSetChanged();
        } else if (isCollect) {
            //去到图片详情
            CollectPicDetailActivity.imgPath = item.getPath();
            Intent intent = new Intent(getContext(), CollectPicDetailActivity.class);
            getContext().startActivity(intent);
        } else {
            //去到图片详情
            PicDetailActivity.imgPath = item.getPath();
            PicDetailActivity.isAlbum = isAlbum;
            Intent intent = new Intent(getContext(), PicDetailActivity.class);
            getContext().startActivity(intent);
        }
    }

    //点击反馈
    private ClickFeedback clickFeedback;

    public interface ClickFeedback {
        void onClickFeedback(boolean isSel, String path);
    }

    public void setClickFeedback(ClickFeedback clickFeedback) {
        this.clickFeedback = clickFeedback;
    }
}
